﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace Agiltastic
{
    public partial class fHandler : FormBase
    {
        private HandlerDao _HandlerDao = new HandlerDao();
        private ClubDao _clubDao = new ClubDao();
        private List<Handler> _listHandler = new List<Handler>();
        private Handler _currentSelected;

        public fHandler()
        {
            InitializeComponent();
            try
            {
                LoadGrid();
                LoadClub();
                EnableControls(false);
                CurrentMode = Mode.DoNothing;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }
        }

        private void LoadGrid()
        {
            _listHandler = _HandlerDao.GetAllHandler();
            dgvHandler.DataSource = _listHandler;
        }

        private void LoadClub()
        {
            cmbClub.DataSource = _clubDao.GetAllClub();
            
        }

        private void EnableControls(bool b)
        {
            txtFirstName.Enabled = b;
            txtLastName.Enabled = b;
            cmbClub.Enabled = b;
            btnSave.Enabled = b;
        }

        private void txtFilter_TextChanged(object sender, EventArgs e)
        {
            dgvHandler.DataSource = _listHandler.FindAll(c => c.Name.ToUpperInvariant().Contains(txtFilter.Text.ToUpperInvariant()));
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
           
            txtLastName.Text = "";
           
            txtFirstName.Text = "";
            _currentSelected = new Handler();
            CurrentMode = Mode.Add;
            EnableControls(true);
        }

        
        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvHandler.SelectedCells.Count > 0)
            {
                var name = (String)dgvHandler.SelectedCells[1].Value;
                if (name != "")
                {
                    _currentSelected = _listHandler.Find(c => c.Name == name);
                    
                    txtLastName.Text = _currentSelected.LastName;
                    txtFirstName.Text = _currentSelected.FirstName;
                    cmbClub.SelectedIndex = cmbClub.FindStringExact(_currentSelected.Club1.Name);
                    CurrentMode = Mode.Edit;
                    EnableControls(true);
                }
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtFirstName.Text))
                MessageBox.Show("The first name cannot be empty");
            else
            {
                _currentSelected.Name = txtLastName.Text + ", " + txtFirstName.Text;
                _currentSelected.LastName = txtLastName.Text;
                _currentSelected.FirstName = txtFirstName.Text;
                _currentSelected.Club = ((Club) cmbClub.SelectedItem).Id;
                _currentSelected.ClubName = ((Club) cmbClub.SelectedItem).Name;
                if (CurrentMode == Mode.Edit)
                {
                    _HandlerDao.Update(_currentSelected);
                }

                if (CurrentMode == Mode.Add)
                {
                    _HandlerDao.Save(_currentSelected);
                }
                LoadGrid();
                EnableControls(false);
            }
        }
    }
}
